package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;

import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;

import com.sz.mes.entity.WorkHours;
import com.sz.mes.dto.WorkHoursDTO;
import com.sz.mes.service.WorkHoursService;
import com.sz.mes.vo.WorkHoursListVO;
import com.sz.mes.excel.model.WorkHoursExportModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 工时 Excel处理Controller
 * @author hem
 * @version 1.0
 * @date 2024-11-28
 * Copyright © szhs
 */
@RestController
@RequestMapping("/workHours/excel")
@BindPermission(name = "工时导入导出", code = "WorkHoursExcel")
@Slf4j
public class WorkHoursExcelController extends BaseExcelFileController {

    @Autowired
    private WorkHoursService workHoursService;

    
    /**
     * 工时列表导出
     *
     * @param response
     * @return
     * @throws Exception
     */
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(WorkHoursDTO workHoursDto, @RequestParam(value = "columns", required = false) List<String> columns,
    												 HttpServletResponse response) throws Exception{
        QueryWrapper<WorkHours> queryWrapper = super.buildQueryWrapperByQueryParams(workHoursDto);
        List<WorkHoursListVO> workHoursList = workHoursService.getViewObjectList(queryWrapper, null, WorkHoursListVO.class);
        if (V.isEmpty(workHoursList)) {
            return new JsonResult(Status.FAIL_OPERATION, "工时列表为空，导出失败");
        }
        String fileName = "工时列表导出_" + D.today() + ".xlsx";
        List<WorkHoursExportModel> dataList = this.entityList2ExcelList(workHoursList);
        ExcelHelper.exportExcel(response, fileName, WorkHoursExportModel.class, columns, dataList);
        return null;
    }

    /**
     * 实体列表转excel列表
     *
     * @param workHoursList
     * @return
     */
    private List<WorkHoursExportModel> entityList2ExcelList(List<WorkHoursListVO> workHoursList) {
        if (V.isEmpty(workHoursList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(workHoursList, WorkHoursExportModel.class);
    }

    @Override
    protected ReadExcelListener getExcelDataListener() {
        return null;
    }
}